<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/13
 * Time: 22:07
 */
namespace app\model;


use think\facade\Db;
use think\Model;

class Goods extends Model
{

    private function _getListField()
    {
        return [
            'goods_id' => 'goodsId',
            'goods_name' => 'name',
            'goods_stock' => 'stock',
            'goods_exchange_integral' => 'exchangeIntegral',
            'goods_pic' => 'pic',
            'goods_status' => 'status',
            'goods_create_time' => 'createTime'
        ];
    }

    public function getGoodsInfoById($goodsId,$field = '')
    {
        $field = $field ?: self::_getListField();
        return Db::table('goods')->where(['goods_id'=>$goodsId])->field($field)->find();
    }
    public function addOneGoods($data)
    {
        $data['goods_create_time'] = time();
        return Db::table('goods')->insertGetId($data);
    }

    public function getGoodsListByCondition($where = [],$order = 'goods_id desc',$field = '')
    {
        $field = $field ?: self::_getListField();
        $list = Db::table('goods')
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate(input('param.pageSize'))
            ->render();
        return $list;
    }

    public function updateGoodsInfoByWhere($where,$data)
    {
        $data['goods_update_time'] = time();
        return Db::table('goods')->where($where)->update($data);
    }

    public static function reduceStock($goodsId,$num = 1)
    {
        return Db::table('goods')->where(['goods_id'=>$goodsId])->dec('goods_stock',$num)->update();
    }

}